<template>
  <div class="entity-form">
    <el-form :model="formData" :rules="rules" label-width="100px" ref="formData">
      <el-form-item label="实体名称" prop="title">
        <el-input clearable v-model="formData.title"></el-input>
      </el-form-item>

      <el-form-item label="物理表名" prop="tableName">
        <el-input clearable v-model="formData.tableName"></el-input>
      </el-form-item>

      <el-form-item label="存入搜索引擎" prop="saveToEs">
        <el-switch v-model="formData.saveToEs"></el-switch>
      </el-form-item>

      <el-form-item label="模块分组" prop="module">
        <el-select clearable placeholder="请选择" v-model="formData.module">
          <el-option :key="item.id" :label="item.title" :value="item.id" v-for="item in modules"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="formData.description"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="submitForm('formData')" type="primary">保存</el-button>
        <el-button @click="resetForm('formData')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  ENTITY_MODULE,
  ENTITY_MODULE_BUSINESS
} from '@/constants/BaseSelectItemCode'
export default {
  name: 'EntityForm',
  components: {},
  data() {
    var checkTableName = (rule, value, callback) => {
      var regular = /^[a-z_]+$/
      setTimeout(() => {
        if (regular.test(value)) {
          callback()
        } else {
          callback(new Error('必须全是小写字母'))
        }
      }, 500)
    }
    return {
      dataId: null,
      modules: [],
      formData: {
        title: null,
        tableName: null,
        saveToEs: false,
        description: null,
        module: ENTITY_MODULE_BUSINESS
      },
      limit: 1,
      rules: {
        title: [
          { required: true, message: '请填写实体名称', trigger: 'blur' },
          { max: 15, message: '最多 15 个字符', trigger: 'blur' }
        ],
        tableName: [
          { required: true, message: '请填写物理表名', trigger: 'blur' },
          {
            min: 5,
            max: 50,
            message: '长度在 5 到 50 个字符',
            trigger: 'blur'
          },
          { validator: checkTableName, trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    if (this.$route.query && Object.keys(this.$route.query).length > 0) {
      this.dataId = this.$route.query.id
      this.getDataById(this.dataId)
    }
    //获取初始化数据
    this.getFormInfo()
  },
  methods: {
    getFormInfo() {
      this.$store
        .dispatch('baseSelectItem/getByPid', ENTITY_MODULE)
        .then(res => {
          this.modules = res
        })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$store
            .dispatch('entity/createOrUpdate', this.formData)
            .then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.$router.push({ path: '/system/entity' })
            })
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    getDataById(dataId) {
      this.$store.dispatch('entity/getById', dataId).then(res => {
        this.formData = res
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.entity-form {
  .el-form {
    padding: 10px;
    margin: 0 auto;
    max-width: 800px;
  }
}
</style>

